#include <iostream>
#include <vector>

using namespace std;

const int beta = 2, p = 3, L = -1, U = 1;

double pow(double x, int n)
{
    double origin = 1;    
    if (n < 0)
    {
	n *= -1;
	x = 1 / x;
    }
    for (int i=0;i<n;i++)
    {
	origin *= x;
    }
    return origin;
}

void add(vector<int> &digits, int pos)
{
    digits[pos] += 1;
    if (digits[pos] == beta)
    {
	digits[pos] = 0;
	add(digits, pos-1);
    }
}

int print_F()
{
    int count = 0;
    vector<int> digits(p);
    for (int i=0;i<p;i++)
    {
	digits[i] = 0;
    }
    digits[0] = 1;
    digits[p-1] = -1;

    int num = pow(beta, p-1);
    for (int i=0;i<num;i++)
    {
	add(digits, p-1);
	for (int j=L;j<=U;j++)
	{
	    double sum = 0;
	    double power = pow(beta, j);
	    for (int k=0;k<p;k++)
	    {
		sum += digits[k] * pow(beta, -1 * k) * power;
	    }
	    cout << sum << endl;
	    count++;
	}	
    }
    return count;
}

int print_sF()
{
    int count = 0;
    vector<int> digits(p);
    for (int i=0;i<p;i++)
    {
	digits[i] = 0;
    }
    digits[0] = 0;
    digits[p-1] = 0;

    int num = pow(beta, p-1) - 1;
    for (int i=0;i<num;i++)
    {
	add(digits, p-1);
	double sum = 0;
	double power = pow(beta, L);
	for (int k=0;k<p;k++)
	{
	    sum += digits[k] * pow(beta, -1 * k) * power;
	}
	cout << sum << endl;
	count++;
    }
    return count;
}

int main()
{
    double UFL = pow(beta, L);
    double OFL = pow(beta, U) * (beta - pow(beta, 1-p));

    cout << "UFL: " << UFL << endl;
    cout << "OFL: " << OFL << endl;
    
    cout << "system F: " << endl;
    cout << "number: " << print_F() << endl;
    cout << "system subnormal F: " << endl;
    cout << "number: " << print_sF() << endl;
    
    return 0;
}
